package simulator.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.List;

import simulator.hardware.Processor;
import simulator.software.Kernel;
import simulator.software.ProcessorRunnable;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

import simulator.Scenario;
import simulator.SchedulingScenario;

public class KernelRenderer extends JComponent
{
   private static final long serialVersionUID = 1L;
   private static final Color defaultColor = new Color(100, 100, 100);
   private Scenario scenario = null;
   private Kernel kernel = null;
   private static Font normalFont = new Font("Arial", Font.PLAIN, 12);

   private Image kernelImage = null;

   public KernelRenderer(Scenario scenario, Kernel kernel)
   {
      this.scenario = scenario;
      this.kernel = kernel;

      //try
      //{
         ImageLoader loader = new ImageLoader();
         kernelImage = loader.load("kernel.png");
         //kernelImage = ImageIO.read(new File("images/kernel.png"));
      //}
      //catch (IOException ioe)
      //{
      //   ioe.printStackTrace();
      //}
   }

   public Dimension getPreferredSize()
   {
      return new Dimension(1000, 30);
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);

      Graphics2D area = (Graphics2D) g;
      int paddingLeft = 10;

      area.setColor(defaultColor);

      g.setFont(normalFont);

      String status = kernel.getStatusDescriptor();
      Image decal = kernelImage;

      g.drawImage(decal, paddingLeft, 0, 72, 24,
            this);
      g.drawString("Operating System", paddingLeft + 80, 10);
      g.drawString(status, paddingLeft + 80, 24);

      Rectangle r = area.getClipBounds();

      if (r != null)
      {
         drawHistory(area.create(250, 0,
               850, 40), kernel);
      }


   }

   public void drawHistory(Graphics g, ProcessorRunnable p)
   {
      Graphics2D area = (Graphics2D) g;
      int quantWidth = 4;
      SchedulingScenario sched = (SchedulingScenario) scenario;

      Processor processor = sched.getComputer().getProcessors().get(0);
      List<ProcessorRunnable> history = processor.getHistory();

      int start = scenario.getCurrentStep() - 180;
      int end = scenario.getCurrentStep();

      for (int step = start; step < end && step < history.size(); step++)
      {
         if (step >= 0 && history.get(step) == p)
         {
            area.setPaint(new GradientPaint(quantWidth * (step - start), 0,
                  new Color(255, 255, 255), quantWidth * (step - start)
                        + quantWidth, 16, new Color(192, 216, 234)));
            area.fillRect(quantWidth * (step - start), 0, quantWidth, 16);
            area.setPaint(new Color(190, 190, 240));
            area.drawRect(quantWidth * (step - start), 0, quantWidth, 16);
         }
         else
         {
            area.setColor(new Color(200, 200, 200));
            area.drawLine(quantWidth * (step - start), 8, quantWidth
                  * (step - start) + quantWidth, 8);
         }
      }
   }
}
